package templateExcel;


import lombok.Setter;

import java.util.Map;

@Setter
public class DoubleDimensionalArrayQuote extends TemplateQuote{

    private String quote;

    private boolean firstIsVertical = true;

    private String startExpression1;
    private String lenExpression1;

    private String startExpression2;
    private String lenExpression2;



    @Override
    public String getQuoteName() {

        if (quote == null) {
            quote = "__double_dimensional_array_" + IdFactory.id();
        }

        return quote;
    }

    @Override
    public void bind(Map<String, Object> values) {

    }

    @Override
    public Object getValue(Map<String, Object> values, int i, int j) {

        if (i < 0) {
            i = 0;
        }

        if (j < 0) {
            j = 0;
        }

        //获取名称
        Object o = values.get(name);

        if (o instanceof Object[][]) {

            Object[][] array = (Object[][]) o;

            if (firstIsVertical) {
                if (array.length > i) {
                    Object[] a = array[i];
                    if (a != null && a.length > j) {
                        return a[j];
                    }
                }
            } else {
                if (array.length > j) {
                    Object[] a = array[j];
                    if (a != null && a.length > i) {
                        return a[i];
                    }
                }
            }

        } else {
            System.err.println(name+" 不是二维数组,不满足预期");
        }


        return null;
    }

    @Override
    public int maxI(Map<String, Object> values) {

        Object o = values.get(name);
        if (o instanceof Object[][]) {
            Object[][] array=((Object[][]) o);
            //直接返回第一个维度
            if (firstIsVertical) {
                return array.length;
            } else {
                int i = 0;

                for (Object[] objects : array) {
                    if (objects != null) {
                        if (objects.length > i) {
                            i = objects.length;
                        }
                    }
                }
                return i;
            }

        } else {
            System.err.println(name+" 不是二维数组,不满足预期");
        }


        return 0;
    }

    @Override
    public int maxJ(Map<String, Object> values) {
        Object o = values.get(name);
        if (o instanceof Object[][]) {
            Object[][] array=((Object[][]) o);
            if (!firstIsVertical) {
                return array.length;
            } else {
                int i = 0;
                for (Object[] objects : array) {
                    if (objects != null) {
                        if (objects.length > i) {
                            i = objects.length;
                        }
                    }
                }
                return i;
            }

        } else {
            System.err.println(name+" 不是二维数组,不满足预期");
        }


        return 0;
    }

}
